


var Circuit = function(paper, points, size) {
	this.paper = paper;
	this.points = points;
	this.size = size;
	this.arrowPath;
	this.circuitLength;
	this.on = true;
	this.time = 6000;
	this.arrows = new Array();
	
	this.pathString = "M";
	for (var i = 0; i < this.points.length; i++)
	{
		if (i != 0)
		{
			this.pathString = this.pathString + "L" + this.points[i][0] + " " + this.points[i][1] + " ";
		}
		else
		{
			this.pathString = this.pathString + this.points[i][0] + " " + this.points[i][1] + " ";
		}
	}
	this.arrowPath = this.paper.path(this.pathString);
	this.circuitLength = this.arrowPath.getTotalLength();
	
	
	
	/* getSwitchHandle returns an array A in this format:
		A[0] - x-coord. of midpoint of longest line segment in path
		A[1] - y-coord. of midpoint of longest line segment in path
		A[2] - slope of longest line segment in path
		A[3] - length of longest line segment in path
	*/
	this.getSwitchHandle = function() {
		var maxI = 0;
		var maxDist = 0;
		for (var i = 0; i < this.points.length-1; i++)
		{
			var dist = Math.sqrt(Math.pow(this.points[i][0]-this.points[i+1][0],2)+Math.pow(this.points[i][1]-this.points[i+1][1],2))
			if (dist > maxDist)
			{
				maxDist = dist;
				maxI = i;
			}
		}
		var data = [(this.points[maxI][0]+this.points[maxI+1][0])/2,(this.points[maxI][1]+this.points[maxI+1][1])/2]
		data.push((this.points[maxI][1]-this.points[maxI+1][1])/(this.points[maxI][0]-this.points[maxI+1][0]));
		data.push(maxDist);
		return data;
		
	}
	
	this.running = function(delay) {
		
		if (this.on)
		{
		setTimeout(function (thisObj) {
			var curArrow = new Circuit.Arrow(thisObj.paper, thisObj.size, thisObj.points,thisObj.arrowPath);
			curArrow.svgArrow.animateAlong(thisObj.arrowPath, thisObj.time/2, true);
			curArrow.svgArrow.show();
			curArrow.animating = true;
			thisObj.arrows.push(curArrow);
			for (var i = 0; i < thisObj.arrows.length; i++)
			{
				if (Math.abs(thisObj.arrows[i].svgArrow.attr("x") + thisObj.arrows[i].svgArrow.attr('width')/2 - thisObj.points[thisObj.points.length-1][0]) < thisObj.arrows[i].svgArrow.attr('width') && 
						Math.abs(thisObj.arrows[i].svgArrow.attr("y") + thisObj.arrows[i].svgArrow.attr('height')/2 - thisObj.points[thisObj.points.length-1][1]) < thisObj.arrows[i].svgArrow.attr('height') && 
						thisObj.arrows[i].animating)
				{
					thisObj.arrows[i].svgArrow.hide();
					thisObj.arrows.splice(i,i);
					break;
				}
			}
			thisObj.running(delay);
		}, delay, this);
		}
		else
		{
			this.pauseAnimation();
			return 1;
			
		}
	}
	
	this.run = function(delay)	{
		this.returnVal = 0;
		var inc = 0;
		while (!this.returnVal && inc < 1)
		{
			inc += 1;
			this.returnVal = this.running(delay);
			
		}
		
	}
	
	this.toggleState = function() {
		if (this.on == true)
		{
			this.on = false;
		}
		else
		{
			this.on = true;
			this.run(this.circuitLength);	// enter main loop
		}
	}
	
	
	
}


Circuit.Arrow = function(paper,size,points,arrowPath,time) {
	this.paper = paper;
	this.points = points;
	this.frontX = 20;
	this.frontY = 0;
	this.backX = -20;
	this.backY = 0;
	this.arrowPath = arrowPath;
	this.angle = -90;
	this.time = time;
	this.animating = false;
	
	
	if (size == 'small')
	{
		this.size = .2;
		this.sizeMax = this.size;	// not gonna be important 'til later (appear/disappear animation)
	}
	if (size == 'medium')
	{
		this.size = .35;
		this.sizeMax = this.size;	// not gonna be important 'til later (appear/disappear animation)
	}
	if (size == 'large')
	{
		this.size = .5;
		this.sizeMax = this.size;	// not gonna be important 'til later (appear/disappear animation)
	}
	
	this.lastPoint = null;	// last point the arrow has passed
	
	this.rotateRelative = function(deg) {
		this.angle += deg;
		this.svgArrow.rotate(deg);
	}
	
	this.rotateAbsolute = function(deg) {
		this.angle = deg-90;
		this.svgArrow.rotate(deg,true);
	}
	
	this.move = function(newFrontX,newFrontY,newBackX,newBackY) {
		
		// rotate arrow to new position
		this.rotAngle = Math.acos($V([1,0]).dot($V([newFrontX-newBackX,newFrontY-newBackY]).toUnitVector()))*180/Math.PI;
		if (newBackY >= newFrontY)
		{
			this.rotAngle = -1*this.rotAngle;
		}
		this.svgArrow.rotate(this.rotAngle, true);
		
	}
	
	
	this.reset = function() {
		this.svgArrow.hide();
		this.svgArrow.attr({x:-30, y:-15});
		this.svgArrow.scale(this.size,this.size,0,0);
		this.svgArrow.translate(this.points[0][0],this.points[0][1]);
		this.rotateRelative(-90);
	}
	
	this.svgArrow = paper.image("arrow.svg", -30, -15, 60, 40);
	this.svgArrow.hide();
	this.svgArrow.scale(this.size,this.size,0,0);
	this.svgArrow.translate(this.points[0][0],this.points[0][1]);
	this.rotateRelative(-90);
	//this.svgArrow.animateAlong(this.arrowPath,this.time,true);
	//this.svgArrow.remove();
	//delete this;
	
	// automatically die at the end of lifespan
	
	
}
